﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class UglyNumberII : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    /**
    https://leetcode.com/problems/ugly-number-ii/

    动态规划解法
     */
    public int NthUglyNumber(int n) {
        if (n <= 0) return 0;
        int[] dp = new int[n];
        dp[0] = 1;
        int t2 = 0, t3 = 0, t5 = 0;
        
        for (int i = 1; i < n; i++){
            dp[i] = Math.Min(dp[t2] * 2, Math.Min(dp[t3] * 3, dp[t5] * 5));
            
            if (dp[i] == dp[t2] * 2) t2++;
            if (dp[i] == dp[t3] * 3) t3++;
            if (dp[i] == dp[t5] * 5) t5++;
        }
        
        return dp[n - 1];
    }
}
